layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户
    table.render({
        elem: '#userList',
        url : '/shiro/user/userlist?ispage=true',
        cellMinWidth : 95,
        page : true,
        height : "full-98",
        limits : [10,15,20,25],
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:100, align:"center"},
            {field: 'username', title: '用户名', width:150},
            {field: 'nickname', title: '昵称', align:'center'},
            {field: 'status', title: '状态',  align:'center',templet:"#isShow"},
            {field: 'email', title: '邮箱', align:'center', minWidth:110},
            {title: '操作', minWidth:100, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });
    //监听指定开关
    form.on('switch(isShow)', function(obj){
        layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        var status=obj.elem.checked ?1 :2;

        var user;
        user = '{"id":"'+this.value +'",';  //登录名
        user += '"status":"'+ status +'"}';  //角色
        $.ajax({
            type: "POST",
            url:"/shiro/user/edituser",
            contentType: "application/json",
            data:  JSON.stringify(JSON.parse(user)),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){

                    });
                }else{
                    alert(r.msg);
                }
            }
        });

    });
    //搜索
    $(".search_btn").on("click",function(){
            table.reload("userListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                        username : $(".searchVal").val()

                }
            })
    });
    //监听工具条
    table.on('tool(userList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'detail'){ //查看
            layer.msg( JSON.stringify(data.id));
        } else if(layEvent === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
               var  dataString = new Array();
                dataString.push(data.id);
                //向服务端发送删除指令
                    $.ajax({
                        type: "GET",
                        url:"/shiro/user/delectUser",
                        contentType: "application/json",
                        data:{"userIds":JSON.stringify(parseInt(dataString))},
                        success: function(r){
                            if(r.code != 0){
                                alert(r.msg);
                            }
                            else {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }
                        }
                    });
                });
        } else if(layEvent === 'edit'){ //编辑
                layer.setArgs("data",JSON.stringify(data.id));
                layer.open({
                    type:2,
                    area:["550px","450px"],
                    title:"用户信息",
                    content:"/view/admin/user/edit"
                });
        }
    });
    //添加用户
    var addNewsIframe;
    $(".addNews_btn").click(function(){
        addNewsIframe = layui.layer.open({
            title : "添加用户",
            type : 2,
            area: ['650px', '450px'],
            content : "/view/admin/user/add",
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },300)
            }
        })
    })

    var $ = layui.$, active = {
        delCheckData: function () { //获取选中数据
            var checkStatus = table.checkStatus('userListTable')
                , data = checkStatus.data;
          //  layer.alert(JSON.stringify(data));
            var  dataString = new Array();
            for(var o in data){
                dataString.push(data[o].id);
            }
            if (dataString === undefined || dataString.length == 0) {
                layer.msg("请选择一条数据");
                return false;
            }
            //向服务端发送删除指令
            $.ajax({
                type: "GET",
                url:"/shiro/user/delectUser",
                contentType: "application/json",
                data:{"userIds":JSON.stringify(parseInt(dataString))},
                success: function(r){
                    if(r.code != 0){
                        layer.msg(r.msg);
                        table.reload("userListTable",{
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            where: {
                                username : $(".searchVal").val()

                            }
                        })
                    }
                    else {
                        table.reload("userListTable",{
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            where: {
                                username : $(".searchVal").val()

                            }
                        })
                    }
                }
            });
        }
    }

    $('.delUser_btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
})